﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Threading;

namespace Components
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Logger
    {
        private static readonly object obj = new object();

        public static string DirPath = "D:\\log";

        public static void SetLogPath(string path)
        {
            DirPath = path;
        }

        /// <summary>  
        /// 操作日志  
        /// </summary>  
        /// <param name="s">日志能容</param>  
        public static void WriteLog(string title, string content)
        {
            WriteLogs(title, content, "操作日志");
        }

        /// <summary>  
        /// 错误日志  
        /// </summary>  
        /// <param name="s">日志内容</param>  
        public static void WriteError(string title, string content)
        {
            WriteLogs(title, content, "错误日志");
        }

        public static void WriteError(Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            while (null != ex)
            {
                sb.Append(string.Format("{0} {1}\r\n{2}\r\nSource:{3}", ex.GetType().Name, ex.Message, ex.StackTrace, ex.Source));
                ex = ex.InnerException;
            }

            WriteLogs("", sb.ToString(), "错误日志");
        }

        public static void WriteSqlCommand(IDbCommand cmd)
        {
            if (null != cmd)
            {
                IDataParameter[] parameter = new IDataParameter[cmd.Parameters.Count];
                cmd.Parameters.CopyTo(parameter, 0);
                BeginTraceSql(cmd.CommandText, parameter);
            }
        }

        private static void BeginTraceSql(string sql, params IDataParameter[] parameter)
        {
            if (!string.IsNullOrEmpty(sql))
            {
                StringBuilder sb = new StringBuilder();

                sb.Append(sql);
                if (parameter != null)
                {
                    foreach (IDataParameter param in parameter)
                    {
                        sb.Append(string.Format("name:{0}, value:{1}", param.ParameterName, param.Value));
                    }
                }
            }
        }


        public static void WriteLogs(string title, string content, string type)
        {
            lock (obj)
            {
                string path = DirPath;
                if (!string.IsNullOrEmpty(path))
                {
                    path = path + "\\" + DateTime.Now.ToString("yyMM");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    path = path + "\\" + DateTime.Now.ToString("dd") + ".txt";
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();
                    }
                    if (File.Exists(path))
                    {
                        StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                        sw.WriteLine(DateTime.Now + " " + title);
                        sw.WriteLine("日志类型：" + type);
                        sw.WriteLine("详情：" + content);
                        sw.WriteLine("----------------------------------------");
                        sw.Close();
                    }
                }
            }
        }
    }
}
